﻿cmake_minimum_required(VERSION 3.8)

project(TensorRTools LANGUAGES CXX CUDA)

# 设置生成 .so 和可执行文件的输出目录
set(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/lib)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/bin)


# 设置 C++ 标准
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 设置构建类型（Debug 或 Release），默认为 Release
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif()

# 设置生成动态库或可执行文件
set(BUILD_SHARED_LIBS ON CACHE BOOL "Build shared libraries")

# 查找 CUDA
find_package(CUDA REQUIRED)

# 查找 OpenCV，如果未找到，则手动设置路径
find_package(OpenCV QUIET)
if(NOT OpenCV_FOUND)
    message(STATUS "OpenCV not found, setting paths manually")
    
    if(WIN32)
        # Windows 上的 OpenCV 库路径（根据你的实际情况修改）
        set(OpenCV_DIR  ${CMAKE_CURRENT_SOURCE_DIR}/../3rd/opencv4.7/lib)
        set(OpenCV_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/../3rd/opencv4.7/include)
        set(OpenCV_LIBS
             ${OpenCV_DIR}/opencv_world470.lib
        )
    else()
        # Linux 上的 OpenCV 库路径（根据你的实际情况修改）
        set(OpenCV_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../3rd/opencv4.7/lib)
        set(OpenCV_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/../3rd/opencv4.7/include)
        set(OpenCV_LIBS
            ${OpenCV_DIR}/libopencv_core.so
            ${OpenCV_DIR}/libopencv_imgproc.so
            ${OpenCV_DIR}/libopencv_highgui.so
        )
    endif()
endif()

# 根据平台设置 TensorRT 路径和库文件
if(WIN32)
    # Windows 上的 TensorRT 库路径（根据你的实际情况修改）
    set(TENSORRT_LIB_DIR    ${CMAKE_CURRENT_SOURCE_DIR}/../3rd/TensorRT/lib)
    set(TENSORRT_INCLUDE_DIR  ${CMAKE_CURRENT_SOURCE_DIR}/../3rd/TensorRT/include)
    set(TENSORRT_LIBS
        ${TENSORRT_LIB_DIR}/nvinfer.lib
        ${TENSORRT_LIB_DIR}/nvonnxparser.lib
        ${TENSORRT_LIB_DIR}/nvparsers.lib
        ${TENSORRT_LIB_DIR}/nvinfer_plugin.lib
        cudnn.lib
        cublas.lib
        cudart.lib
    )
else()
    # Linux 上的 TensorRT 库路径（根据你的实际情况修改）
    set(TENSORRT_LIB_DIR "/usr/lib/aarch64-linux-gnu/")
    set(TENSORRT_INCLUDE_DIR "/usr/include/aarch64-linux-gnu/")
    set(TENSORRT_LIBS
        ${TENSORRT_LIB_DIR}/libnvinfer.so
        ${TENSORRT_LIB_DIR}/libnvonnxparser.so
        ${TENSORRT_LIB_DIR}/libnvparsers.so
        ${TENSORRT_LIB_DIR}/libnvinfer_plugin.so
    )
endif()

# 包含路径
include_directories(
    ${TENSORRT_INCLUDE_DIR}
    ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}
    ${CMAKE_CURRENT_SOURCE_DIR}/include  # 添加 include 目录
    ${OpenCV_INCLUDE_DIRS}  # 添加 OpenCV 的包含目录
)

include_directories(${CMAKE_CURRENT_SOURCE_DIR})

# 指定源文件和头文件
file(GLOB SRC_FILES "src/*.cpp")  # 收集 src 目录下的所有 .cpp 文件

set(SOURCES   
    infer.cu
    yolo.cu
    tensorRTTools.cpp
    ${SRC_FILES}  # 添加收集到的 .cpp 文件
)
set(HEADERS
    cpm.hpp
    infer.hpp
    yolo.hpp
    tensorRTTools.h
    include/BYTETracker.h
)


# 根据 BUILD_SHARED_LIBS 选项生成动态库和/或可执行文件
if(BUILD_SHARED_LIBS)
    # 生成动态库
    add_library(${PROJECT_NAME} SHARED ${SOURCES} ${HEADERS})
    target_compile_definitions(${PROJECT_NAME} PRIVATE TENSORRTTOOLS_EXPORTS)

    # 生成可执行文件，链接动态库
    # add_executable(${PROJECT_NAME}_exe main.cpp)
    # target_link_libraries(${PROJECT_NAME}_exe ${PROJECT_NAME} ${TENSORRT_LIBS} ${CUDA_LIBRARIES} ${CUDA_CUBLAS_LIBRARIES} ${OpenCV_LIBS})
else()
    # 只生成可执行文件
    add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS})

    # 设置可执行文件的输出路径
    set_target_properties(${PROJECT_NAME} PROPERTIES
        RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin
    )
endif()

# 链接 TensorRT 和 OpenCV 库
target_link_libraries(${PROJECT_NAME} 
				${TENSORRT_LIBS} 
				${CUDA_LIBRARIES} 
				${CUDA_CUBLAS_LIBRARIES} 
				${OpenCV_LIBS})

# 设置编译选项
set_target_properties(${PROJECT_NAME} PROPERTIES
    CUDA_SEPARABLE_COMPILATION ON
)

# 根据平台设置 CUDA 架构
if(WIN32)
    set_target_properties(${PROJECT_NAME} PROPERTIES
        CUDA_ARCHITECTURES "75"
    )
else()
    set_target_properties(${PROJECT_NAME} PROPERTIES
        CUDA_ARCHITECTURES "75"
    )
endif()


# install target and libraries
if(WIN32)
    # 设置动态库/静态库生成路径
    set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/../x64/Release)
else()
    set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/../bin/ARM64/jeston)
endif()

# 设置生成的库文件的安装路径
install(TARGETS ${PROJECT_NAME} 
    RUNTIME DESTINATION bin   # 可执行文件放到 BIN 目录
    LIBRARY DESTINATION .   # 动态库放到 LIB 目录
    ARCHIVE DESTINATION .)  # 静态库（如果有）放到 LIB 目录
	
# 拷贝依赖库到 lib 文件夹
install(FILES  ${TENSORRT_LIBS} DESTINATION .)
#install(FILES  ${OpenCV_LIBS} DESTINATION ./lib)
#install(DIRECTORY workspace DESTINATION ./lib)
